package com.kcsj.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.Count;
import com.kcsj.domain.DelayedApplication;
import com.kcsj.service.DelayedApplicationService;
import com.kcsj.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * @author 傅立贤
 * 28th Nov. 2020
 * {
 *         "order":{
 *             "orderNo":"01"
 *         },
 *         "delayDays":5,
 *          "delayOfMoney":500
 * }
 */
@WebServlet("/delayedOrder.ctl")
public class DelayedApplicationController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //将request body转换为JSON字串
        String req_json = JSONUtil.getJSON(request);
        if (req_json.equals("")) {
            JSONObject message = MessageHelper.addMessage("请输入延期天数");
            response.getWriter().println(message);
            return;
        }try {
            //将请求转为JSONobject对象
            DelayedApplication delayedApplication = JSON.parseObject(req_json,DelayedApplication.class);
            delayedApplication.setApplicationStatus("延期申请待处理中");
            DelayedApplicationService.getInstance().add(delayedApplication);
            JSONObject message = MessageHelper.addMessage("已申请延迟订单");
            response.getWriter().println(message);
        } catch (SQLException e) {
            e.printStackTrace();
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch (Exception e){
            e.printStackTrace();
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String condition = request.getParameter("condition");
        try {
            if(condition!=null) {
                JSONObject jsonObject = JSONObject.parseObject(condition);
                List<Condition> conditionList = ControllerHelper.getConditions(jsonObject);
                System.out.println(conditionList);
                this.responseDelayedApplications(response,conditionList);
            }else{
                this.responseDelayedApplications(response,null);
            }
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端请求的信息
        String orderNo = request.getParameter("orderNo");
        try {
            //到数据库表中删除对应的教师
            DelayedApplicationService.getInstance().delete(orderNo);
            JSONObject message = MessageHelper.addMessage("删除成功");
            response.getWriter().println(message);
        } catch (SQLException e) {
            e.printStackTrace();
            MessageHelper.dateBaseError();
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    private void responseDelayedApplication(String orderNo, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //根据id查找学生
        DelayedApplication delayedApplication = DelayedApplicationService.getInstance().find(orderNo);
        System.out.println(delayedApplication);
        String delayedApplication_json = JSON.toJSONString(delayedApplication);
        JSONObject data = MessageHelper.addData(delayedApplication_json);
        //响应department_json到前端
        response.getWriter().println(data);
    }

    //按查询条件响应延期对象
    private void responseDelayedApplications(HttpServletResponse response,
                                             List<Condition> conditionList)
            throws ServletException, IOException, SQLException {
        Collection<Count> counts = DelayedApplicationService.getInstance().countByStatus();
        String counts_json =
                JSON.toJSONString(counts, SerializerFeature.DisableCircularReferenceDetect);
        Collection<Count> countsByDay = DelayedApplicationService.getInstance().countByDay();
        String countsByDay_json =
                JSON.toJSONString(countsByDay, SerializerFeature.DisableCircularReferenceDetect);
        //获得所有延期
        Collection<DelayedApplication> delayedApplications =
                DelayedApplicationService.getInstance().
                findAll(conditionList);
        String delayedApplications_json =
                JSON.toJSONString(delayedApplications, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(delayedApplications_json);
        data.put("counts",counts_json);
        data.put("countsByDay",countsByDay_json);
        //响应departments_json到前端
        response.getWriter().println(data);
    }
}
